home
***
CD-ROM
|
disk
|
FTP
|
other
***
search
/
SGI Developer Toolbox 6.1
/
SGI Developer Toolbox 6.1 - Disc 4.iso
/
src
/
haeberli
/
compexp
/
compexp.c
< prev
next >
Wrap
C/C++ Source or Header
|
1994-08-01
|
8KB
|
354 lines
/*
* Copyright 1991, 1992, 1993, 1994, Silicon Graphics, Inc.
* All Rights Reserved.
*
* This is UNPUBLISHED PROPRIETARY SOURCE CODE of Silicon Graphics, Inc.;
* the contents of this file may not be disclosed to third parties, copied or
* duplicated in any form, in whole or in part, without the prior written
* permission of Silicon Graphics, Inc.
*
* RESTRICTED RIGHTS LEGEND:
* Use, duplication or disclosure by the Government is subject to restrictions
* as set forth in subdivision (c)(1)(ii) of the Rights in Technical Data
* and Computer Software clause at DFARS 252.227-7013, and/or in similar or
* successor clauses in the FAR, DOD or NASA FAR Supplement. Unpublished -
* rights reserved under the Copyright Laws of the United States.
*/
/*
* comp -
* A compression explorer.
*
* Paul Haeberli - 1991
*/
#include "stdio.h"
#include "gl.h"
#include "device.h"
#include "resource.h"
#include "showcaseui.h"
int compdecomp();
int compress(unsigned long *lbuf, int ixsize, int iysize,
unsigned char *cbuf, int cbuflen, int thchroma, int thflat, int thedge);
void decompress(unsigned char *cbuf, unsigned long *lbuf, int doblend);
float getvalval();
#define IM (2)
#define DRAW_BACK (0x1)
#define DRAW_IMAGE (0x2)
#define SCALE_CHROMA (120) /* default 30 */
#define SCALE_FLAT (200) /* default 15 */
#define SCALE_EDGE (200) /* default 45 */
Button *quitbut;
Button *mainbed;
TextBox *tbchroma, *tbflat, *tbedge, *tbbpp;
Valuator *vthchroma, *vthflat, *vthedge;
long xorg, yorg;
unsigned long *orig;
unsigned long *comp;
int ixsize, iysize;
int cxsize, cysize;
int dxsize, dysize;
int zfactor;
int debug = 1;
main(argc, argv)
int argc;
char **argv;
{
long dev, mx, my;
short val, smx, smy;
FILE *inf;
if(argc<2) {
inf = res_fopen("#t.rgb");
if(inf) {
res_fclose(inf);
initialize("#t.rgb");
} else {
fprintf(stderr,"usage: comp in.rgb\n");
exit(1);
}
} else {
initialize(argv[1]);
}
activate();
drawscene(DRAW_BACK|DRAW_IMAGE);
while (1) {
switch (dev = qread(&val)) {
case LEFTMOUSE:
qread(&smx); qread(&smy);
mx = smx - xorg;
my = smy - yorg;
if (val == UIBUTTONDOWN) {
sginap(8);
if (qtest() == dev) {
short tmp;
qread(&tmp); qread(&tmp); qread(&tmp);
val = UIBUTTONCLICK;
}
}
if (handleevents(mx, my, dev, (long) val))
drawscene(DRAW_IMAGE);
break;
case MOUSEX:
mx = val - xorg;
break;
case MOUSEY:
my = val - yorg;
locatemouseevents(mx,my);
break;
case REDRAW:
getorigin(&xorg,&yorg);
drawscene(DRAW_BACK|DRAW_IMAGE);
break;
}
}
}
long conx, cony, conhi;
initialize(iname)
char *iname;
{
long xsize, ysize;
long tmp, xmax, ymax;
sizeofimage(iname,&ixsize,&iysize);
zfactor = 3;
xmax = getgdesc(GD_XPMAX)-250;
ymax = getgdesc(GD_YPMAX)-70;
while(ixsize*zfactor > xmax || iysize*zfactor > ymax)
zfactor--;
if(zfactor <= 0)
zfactor = 1;
cxsize = 4*(ixsize/4);
cysize = 4*(iysize/4);
dxsize = cxsize*zfactor;
dysize = cysize*zfactor;
xsize = dxsize+3*60+20;
ysize = dysize+20;
if(ysize<410)
ysize = 410;
conx = dxsize+30;
cony = 40;
conhi = 270;
ymax = 300;
quitbut = newpushbut(xsize-10-75,10);
vthchroma = newval(conx+0*60,cony+40,cony+conhi,0,15,0);
vthflat = newval(conx+1*60,cony+40,cony+conhi,0,15,0);
vthedge = newval(conx+2*60,cony+40,cony+conhi,0,15,0);
tbchroma = newtb(vthchroma->xmin-7,cony+10,vthchroma->xmin+28);
tbflat = newtb(vthflat->xmin-7,cony+10,vthflat->xmin+28);
tbedge = newtb(vthedge->xmin-7,cony+10,vthedge->xmin+28);
tbbpp = newtb(conx+90,conhi+cony+30,conx+150);
tmp = (long) ((vthedge->ymax - vthedge->ymin)/2 + vthedge->ymin);
mainbed = newbed(10-IM,10-IM,10+dxsize+IM-1,10+dysize+IM-1);
autoprefsize(xsize,ysize);
winopen("Compression Explorer");
getorigin(&xorg,&yorg);
RGBmode();
gconfig();
cpack(0xe0e0e0);
clear();
frontbuffer(0);
orig = (unsigned long *)longimagedata(iname);
}
activate()
{
initbut();
inittb();
qdevice(LEFTMOUSE);
qdevice(MOUSEX);
qdevice(MOUSEY);
tie(LEFTMOUSE, MOUSEX, MOUSEY);
loadbut(quitbut,"Quit");
enableval(vthchroma);
enableval(vthflat);
enableval(vthedge);
loadtb(tbchroma,"10");
loadtb(tbflat,"5");
loadtb(tbedge,"20");
loadtb(tbbpp,"0.0");
}
int followslider(long val, long my, Valuator *slider, Valuator *link, int cmp)
{
int oy;
oy = -1000;
if (val == UIBUTTONUP) return 0;
handleval(slider, LEFTMOUSE, val);
handleval(slider, MOUSEY, my+yorg);
while ((getbutton(LEFTMOUSE)) || (val == UIBUTTONCLICK)) {
my = getvaluator(MOUSEY);
if(my != oy) {
oy = my;
handleval(slider, MOUSEY, my);
drawval(slider);
if(link && cmp*getvalval(slider)<cmp*getvalval(link)) {
handleval(link, MOUSEY, my);
drawval(link);
}
updateallnumbers();
val = UIBUTTONUP;
}
}
handleval(slider, LEFTMOUSE, val);
if(link)
handleval(link, LEFTMOUSE, my);
updateallnumbers();
return 1;
}
handleevents(long mx, long my, long dev, long val)
{
if (selectedpressbut(quitbut,mx,my,val)) {
exit(0);
} else if (inval(vthchroma, mx, my)) {
return followslider(val,my,vthchroma,0,0);
} else if (inval(vthflat, mx, my)) {
return followslider(val,my,vthflat,vthedge,-1);
} else if (inval(vthedge, mx, my)) {
return followslider(val,my,vthedge,vthflat,1);
} else
return 0;
}
updatenumber(v,t,scaleval)
Valuator *v;
TextBox *t;
int scaleval;
{
char c[128];
int i;
i = scaleval*getvalval(v)+0.5;
sprintf(c,"%3d",i);
loadtb(t,c);
}
updateallnumbers()
{
updatenumber(vthchroma,tbchroma,SCALE_CHROMA);
updatenumber(vthflat,tbflat,SCALE_FLAT);
updatenumber(vthedge,tbedge,SCALE_EDGE);
drawtb(tbflat);
drawtb(tbchroma);
drawtb(tbedge);
}
float bpp;
drawscene(how)
int how;
{
char c[4];
char str[128];
long thchroma, thflat, thedge;
long tmp;
if(how & DRAW_BACK) {
cpack(0xd5d5d5);
clear();
}
drawbut(quitbut);
drawval(vthchroma);
drawval(vthflat);
drawval(vthedge);
drawboldlabel("chroma",vthchroma->xmin-14,vthchroma->ymax+10);
drawboldlabel("flat",vthflat->xmin,vthflat->ymax+10);
drawboldlabel("edge",vthedge->xmin-5,vthedge->ymax+10);
drawboldlabel("Bits Per Pix:",conx-10,cony+conhi+39);
sprintf(str,"Zoom factor: %d",zfactor);
drawboldlabel(str,conx-10,cony+conhi+39+30);
drawborder(mainbed);
if(how & DRAW_IMAGE)
drawmyimage(mainbed,getvalval(vthchroma),getvalval(vthflat),getvalval(vthedge));
updateallnumbers();
sprintf(c,"%3f",bpp);
loadtb(tbbpp,c);
drawtb(tbbpp);
}
float cchroma, cflat, cedge;
drawmyimage(b,fc,ff,fe)
Button *b;
float ff, fc, fe;
{
int thf, thc, the;
if(ff != cflat || fc != cchroma || fe != cedge) {
percentdone(50.0);
if(!comp)
comp = (unsigned long *)malloc(cxsize*cysize*sizeof(long));
thc = SCALE_CHROMA*fc+0.5;
thf = SCALE_FLAT*ff+0.5;
the = SCALE_EDGE*fe+0.5;
bpp = compdecomp(orig,ixsize,iysize,comp,thc,thf,the);
bpp = bpp*8/(ixsize*iysize);
cchroma = fc;
cflat = ff;
cedge = fe;
percentdone(100.0);
}
rectzoom((float)zfactor,(float)zfactor);
lrectwrite(IM+b->xmin,IM+b->ymin,IM+b->xmin+cxsize-1,IM+b->ymin+cysize-1,comp);
}
long lastmx, lastmy;
locatemouseevents(long mx, long my)
{
if ((mx == lastmx) && (my == lastmy))
return;
locatebut(quitbut,mx,my);
locateval(vthchroma,mx,my);
locateval(vthflat,mx,my);
locateval(vthedge,mx,my);
}
/*
* compression stuff follows
*
*/
unsigned char *cbuf;
int cbuflen;
int compdecomp(orig,ixsize,iysize,comp,thc,thf,the)
unsigned long *orig;
int ixsize, iysize;
unsigned long *comp;
int thc, thf, the;
{
int buflen;
if(!cbuf) {
cbuflen = ixsize*iysize;
cbuf = (unsigned char *)malloc(cbuflen);
}
if(debug)
printf("comp params: chroma %d flat %d edge %d\n",thc,thf,the);
buflen = compress(orig,ixsize,iysize,cbuf,cbuflen,thc,thf,the);
decompress(cbuf,comp,2);
return buflen;
}